home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Scout-src.lha / source / objects / scout_windows.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-09-17  |  17.0 KB  |  472 lines

  1. /**
  2.  * Scout - The Amiga System Monitor
  3.  *
  4.  *------------------------------------------------------------------
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * You must not use this source code to gain profit of any kind!
  21.  *
  22.  *------------------------------------------------------------------
  23.  *
  24.  * @author Andreas Gelhausen
  25.  * @author Richard Körber <rkoerber@gmx.de>
  26.  */
  27.  
  28. #include "system_headers.h"
  29.  
  30. struct WindowsCallbackUserData {
  31.     APTR ud_Tree;
  32.     ULONG ud_Count;
  33. };
  34.  
  35. static __asm __saveds LONG wintree_confunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_ConstructMessage *msg, register __a0 struct Hook *hook)
  36. {
  37.     return AllocListEntry(msg->MemPool, msg->UserData, sizeof(struct WindowEntry));
  38. }
  39.  
  40. MakeHook(wintree_conhook, wintree_confunc);
  41.  
  42. static __asm __saveds LONG wintree_desfunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_ConstructMessage *msg, register __a0 struct Hook *hook)
  43. {
  44.     FreeListEntry(msg->MemPool, &msg->UserData);
  45.  
  46.     return 0;
  47. }
  48.  
  49. MakeHook(wintree_deshook, wintree_desfunc);
  50.  
  51. static __asm __saveds LONG wintree_dspfunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_DisplayMessage *msg, register __a0 struct Hook *hook)
  52. {
  53.     static UBYTE *empty="\0";
  54.  
  55.     if (msg->TreeNode != NULL) {
  56.         struct WindowEntry *we = msg->TreeNode->tn_User;
  57.  
  58.         if (stricmp(we->we_Type, "SCREEN") == 0) {
  59.             msg->Array[0] = we->we_Address;
  60.             msg->Array[1] = we->we_Position;
  61.             msg->Array[2] = we->we_Size;
  62.             msg->Array[3] = we->we_Flags;
  63.             msg->Array[4] = empty;
  64.             msg->Array[5] = we->we_Title;
  65.             msg->Preparse[0] = MUIX_PH;
  66.             msg->Preparse[1] = MUIX_PH;
  67.             msg->Preparse[2] = MUIX_PH;
  68.             msg->Preparse[3] = MUIX_PH;
  69.             msg->Preparse[4] = MUIX_PH;
  70.             msg->Preparse[5] = MUIX_PH;
  71.         } else if (stricmp(we->we_Type, "WINDOW") == 0) {
  72.             msg->Array[0] = we->we_Address;
  73.             msg->Array[1] = we->we_Position;
  74.             msg->Array[2] = we->we_Size;
  75.             msg->Array[3] = we->we_Flags;
  76.             msg->Array[4] = we->we_IDCMP;
  77.             msg->Array[5] = we->we_Title;
  78.         }
  79.     } else {
  80.         msg->Array[0] = "Address";
  81.         msg->Array[1] = "Pos(x,y)";
  82.         msg->Array[2] = "Size(w,h)";
  83.         msg->Array[3] = "Flags";
  84.         msg->Array[4] = "IDCMP";
  85.         msg->Array[5] = "Title";
  86.         msg->Preparse[0] = MUIX_B;
  87.         msg->Preparse[1] = MUIX_B;
  88.         msg->Preparse[2] = MUIX_B;
  89.         msg->Preparse[3] = MUIX_B;
  90.         msg->Preparse[4] = MUIX_B;
  91.         msg->Preparse[5] = MUIX_B;
  92.     }
  93.  
  94.     return 0;
  95. }
  96.  
  97. MakeHook(wintree_dsphook, wintree_dspfunc);
  98.  
  99. static __asm __saveds LONG wintree_findfunc(register __a2 Object *obj, register __a1 struct MUIP_NListtree_FindUserDataMessage *msg, register __a0 struct Hook *hook)
  100. {
  101.     struct WindowEntry *we;
  102.  
  103.     we = (struct WindowEntry *)msg->UserData;
  104.  
  105.     if (we) {
  106.         if (stricmp(we->we_Type, "SCREEN") == 0) {
  107.             return stricmp((UBYTE *)msg->User, we->we_ScreenAddress);
  108.         } else {
  109.             return ~0;
  110.         }
  111.     } else {
  112.         return ~0;
  113.     }
  114. }
  115.  
  116. MakeHook(wintree_findhook, wintree_findfunc);
  117.  
  118. void CloseScreenAll( struct Screen *screen )
  119. {
  120.     struct Window *win, *nextwin;
  121.  
  122.     win = screen->FirstWindow;
  123.     while (win) {
  124.         nextwin = win->NextWindow;
  125.         CloseWindow(win);
  126.         win = nextwin;
  127.     }
  128.     CloseScreen(screen);
  129. }
  130.  
  131. static void ReceiveList( void (* callback)( struct WindowEntry *we, void *userData ),
  132.                          void *userData )
  133. {
  134.     struct WindowEntry *we;
  135.  
  136.     if (we = tbAllocVecPooled(globalPool, sizeof(struct WindowEntry))) {
  137.         if (SendDaemon("GetWinList")) {
  138.             while (ReceiveDecodedEntry((UBYTE *)we, sizeof(struct WindowEntry))) {
  139.                 callback(we, userData);
  140.             }
  141.         }
  142.  
  143.         tbFreeVecPooled(globalPool, we);
  144.     }
  145. }
  146.  
  147. static void IterateList( void (* callback)( struct WindowEntry *we, void *userData ),
  148.                          void *userData )
  149. {
  150.     struct MinList tmplist;
  151.     struct WindowEntry *we, *_we;
  152.     ULONG lock;
  153.     struct Screen *screen;
  154.  
  155.     NewList((struct List *)&tmplist);
  156.  
  157.     lock = LockIBase(0);
  158.  
  159.     screen = FIRSTSCREEN;
  160.  
  161.     while (screen) {
  162.         if (we = AllocVec(sizeof(struct WindowEntry), MEMF_PUBLIC)) {
  163.             struct Window *window;
  164.  
  165.             we->we_Addr = (APTR)screen;
  166.  
  167.             _snprintf(we->we_Address, sizeof(we->we_Address), "$%08lx", screen);
  168.             _snprintf(we->we_Position, sizeof(we->we_Position), "%4ld,%-4ld", screen->LeftEdge, screen->TopEdge);
  169.             _snprintf(we->we_Size, sizeof(we->we_Size), "%4ld,%-4ld", screen->Width, screen->Height);
  170.             _snprintf(we->we_Flags, sizeof(we->we_Flags), "$%04lx", screen->Flags);
  171.             stccpy(we->we_IDCMP, "---------", sizeof(we->we_IDCMP));
  172.             _snprintf(we->we_Title, sizeof(we->we_Title), "%-."TEXT_LENGTH_CHAR"s", nonetest(screen->Title));
  173.             _snprintf(we->we_ScreenAddress, sizeof(we->we_ScreenAddress), "$%08lx", screen);
  174.             stccpy(we->we_Type, "SCREEN", sizeof(we->we_Type));
  175.  
  176.             AddTail((struct List *)&tmplist, (struct Node *)we);
  177.  
  178.             if (window = screen->FirstWindow) {
  179.                 while (window) {
  180.                     if (we = AllocVec(sizeof(struct WindowEntry), MEMF_PUBLIC)) {
  181.                         we->we_Addr = (APTR)window;
  182.                         _snprintf(we->we_Address, sizeof(we->we_Address), "$%08lx", window);
  183.                         _snprintf(we->we_Position, sizeof(we->we_Position), "%4ld,%-4ld", window->LeftEdge, window->TopEdge);
  184.                         _snprintf(we->we_Size, sizeof(we->we_Size), "%4ld,%-4ld", window->Width, window->Height);
  185.                         _snprintf(we->we_Flags, sizeof(we->we_Flags), "$%08lx", window->Flags);
  186.                         _snprintf(we->we_IDCMP, sizeof(we->we_IDCMP), "$%08lx", window->IDCMPFlags);
  187.                         _snprintf(we->we_Title, sizeof(we->we_Title), "%-."TEXT_LENGTH_CHAR"s", nonetest(window->Title));
  188.                         _snprintf(we->we_ScreenAddress, sizeof(we->we_ScreenAddress), "$%08lx", screen);
  189.                         stccpy(we->we_Type, "WINDOW", sizeof(we->we_Type));
  190.  
  191.                         AddTail((struct List *)&tmplist, (struct Node *)we);
  192.                     }
  193.  
  194.                     window = window->NextWindow;
  195.                 }
  196.             }
  197.         }
  198.  
  199.         screen = screen->NextScreen;
  200.     }
  201.  
  202.     UnlockIBase(lock);
  203.  
  204.     ITERATE_CHANGING_LIST(&tmplist, struct WindowEntry *, we, _we) {
  205.         callback(we, userData);
  206.         FreeVec(we);
  207.     }
  208. }
  209.  
  210. static void UpdateCallback( struct WindowEntry *we,
  211.                             void *userData )
  212. {
  213.     struct WindowsCallbackUserData *ud = (struct WindowsCallbackUserData *)userData;
  214.     struct MUI_NListtree_TreeNode *parent;
  215.     ULONG flags;
  216.  
  217.     flags = 0;
  218.     if (stricmp(we->we_Type, "SCREEN") == 0) flags = TNF_LIST | TNF_OPEN;
  219.     if (stricmp(we->we_Type, "WINDOW") == 0) flags = 0;
  220.  
  221.     parent = (struct MUI_NListtree_TreeNode *)DoMethod(ud->ud_Tree, MUIM_NListtree_FindUserData, MUIV_NListtree_FindUserData_ListNode_Root, we->we_ScreenAddress, MUIV_NListtree_FindUserData_Flag_StartNode);
  222.     DoMethod(ud->ud_Tree, MUIM_NListtree_Insert, we->we_Title, we, parent, MUIV_NListtree_Insert_PrevNode_Sorted, flags);
  223.     ud->ud_Count++;
  224. }
  225.  
  226. static void PrintCallback( struct WindowEntry *we,
  227.                            void *userData )
  228. {
  229.     if (stricmp(we->we_Type, "SCREEN") == 0) {
  230.         PrintFOneLine((BPTR)userData, " %s %s %s %-9.9s %s %-26.26s\n", we->we_Address, we->we_Position + 2, we->we_Size + 2, we->we_Flags + 2, we->we_IDCMP + 2, we->we_Title + 2);
  231.     } else {
  232.         PrintFOneLine((BPTR)userData, " %s %s %s %-9.9s %s %-26.26s\n", we->we_Address, we->we_Position, we->we_Size, we->we_Flags, we->we_IDCMP, we->we_Title);
  233.     }
  234. }
  235.  
  236. static void SendCallback( struct WindowEntry *we,
  237.                           void *userData )
  238. {
  239.     SendEncodedEntry((UBYTE *)we, sizeof(struct WindowEntry));
  240. }
  241.  
  242. static ULONG __saveds mNew( struct IClass *cl,
  243.                             Object *obj,
  244.                             struct opSet *msg )
  245. {
  246.     APTR winlist, wintree, wintext, updateButton, printButton, closeButton, frontButton, moreButton, exitButton;
  247.  
  248.     if (obj = (Object *)DoSuperNew(cl, obj,
  249.         MUIA_HelpNode, WindowsText,
  250.         MUIA_Window_ID, MakeID('W','I','N','D'),
  251.         WindowContents, VGroup,
  252.  
  253.             Child, winlist = MyNListtreeObject(&wintree, "BAR,BAR P=" MUIX_C ",BAR P=" MUIX_C ",BAR,BAR,BAR", &wintree_conhook, &wintree_deshook, &wintree_dsphook, NULL, &wintree_findhook, 5),
  254.             Child, wintext = MyTextObject(),
  255.  
  256.             Child, MyVSpace(4),
  257.  
  258.             Child, HGroup, MUIA_Group_SameSize, TRUE,
  259.                 Child, updateButton = MakeButton(txtUpdate),
  260.                 Child, printButton  = MakeButton(txtPrint),
  261.                 Child, closeButton  = MakeButton(txtClose),
  262.                 Child, frontButton  = MakeButton(txtToFront),
  263.                 Child, moreButton   = MakeButton(txtMore),
  264.                 Child, exitButton   = MakeButton(txtExit),
  265.             End,
  266.         End,
  267.         TAG_MORE, msg->ops_AttrList))
  268.     {
  269.         struct WindowsWinData *wwd = INST_DATA(cl, obj);
  270.         APTR parent;
  271.  
  272.         wwd->wwd_WindowTree = wintree;
  273.         wwd->wwd_WindowText = wintext;
  274.         wwd->wwd_CloseButton = closeButton;
  275.         wwd->wwd_FrontButton = frontButton;
  276.         wwd->wwd_MoreButton = moreButton;
  277.  
  278.         parent = (APTR)GetTagData(MUIA_Window_ParentWindow, (ULONG)NULL, msg->ops_AttrList);
  279.  
  280.         set(obj, MUIA_Window_Title, MyGetWindowTitle("SCREENS & WINDOWS", wwd->wwd_Title, sizeof(wwd->wwd_Title)));
  281.         set(obj, MUIA_Window_ActiveObject, winlist);
  282.         set(moreButton, MUIA_Disabled, TRUE);
  283.  
  284.         DoMethod(parent,       MUIM_Window_AddChildWindow, obj);
  285.         DoMethod(obj,          MUIM_Notify, MUIA_Window_CloseRequest,   TRUE,           MUIV_Notify_Application, 5, MUIM_Application_PushMethod, parent, 2, MUIM_Window_RemChildWindow, obj);
  286.         DoMethod(wintree,      MUIM_Notify, MUIA_NListtree_Active,      MUIV_EveryTime, obj,                     1, MUIM_WindowsWin_ListChange);
  287.         DoMethod(wintree,      MUIM_Notify, MUIA_NListtree_DoubleClick, MUIV_EveryTime, obj,                     1, MUIM_WindowsWin_More);
  288.         DoMethod(updateButton, MUIM_Notify, MUIA_Pressed,               FALSE,          obj,                     1, MUIM_WindowsWin_Update);
  289.         DoMethod(printButton,  MUIM_Notify, MUIA_Pressed,               FALSE,          obj,                     1, MUIM_WindowsWin_Print);
  290.         DoMethod(closeButton,  MUIM_Notify, MUIA_Pressed,               FALSE,          obj,                     1, MUIM_WindowsWin_Close);
  291.         DoMethod(frontButton,  MUIM_Notify, MUIA_Pressed,               FALSE,          obj,                     1, MUIM_WindowsWin_ToFront);
  292.         DoMethod(moreButton,   MUIM_Notify, MUIA_Pressed,               FALSE,          obj,                     1, MUIM_WindowsWin_More);
  293.         DoMethod(exitButton,   MUIM_Notify, MUIA_Pressed,               FALSE,          obj,                     3, MUIM_Set, MUIA_Window_CloseRequest, TRUE);
  294.     }
  295.  
  296.     return (ULONG)obj;
  297. }
  298.  
  299. static ULONG __saveds mDispose( struct IClass *cl,
  300.                                 Object *obj,
  301.                                 struct opSet *msg )
  302. {
  303.     struct WindowsWinData *wwd = INST_DATA(cl, obj);
  304.  
  305.     set(obj, MUIA_Window_Open, FALSE);
  306.     DoMethod(wwd->wwd_WindowTree, MUIM_NListtree_Clear, NULL, 0);
  307.  
  308.     return (DoSuperMethodA(cl, obj, msg));
  309. }
  310.  
  311. static ULONG __saveds mUpdate( struct IClass *cl,
  312.                                Object *obj,
  313.                                Msg msg )
  314. {
  315.     struct WindowsWinData *wwd = INST_DATA(cl, obj);
  316.     struct WindowsCallbackUserData ud;
  317.  
  318.     ApplicationSleep(TRUE);
  319.     set(wwd->wwd_WindowTree, MUIA_NListtree_Quiet, TRUE);
  320.     DoMethod(wwd->wwd_WindowTree, MUIM_NListtree_Clear, NULL, 0);
  321.  
  322.     ud.ud_Tree = wwd->wwd_WindowTree;
  323.     ud.ud_Count = 0;
  324.  
  325.     if (clientstate) {
  326.         ReceiveList(UpdateCallback, &ud);
  327.     } else {
  328.         IterateList(UpdateCallback, &ud);
  329.     }
  330.  
  331.     MySetContents(wwd->wwd_WindowText, "");
  332.  
  333.     set(wwd->wwd_WindowTree, MUIA_NListtree_Quiet, FALSE);
  334.     set(wwd->wwd_WindowTree, MUIA_NListtree_Active, MUIV_NListtree_Active_Off);
  335.     set(wwd->wwd_CloseButton, MUIA_Disabled, TRUE);
  336.     set(wwd->wwd_FrontButton, MUIA_Disabled, TRUE);
  337.     set(wwd->wwd_MoreButton, MUIA_Disabled, TRUE);
  338.     ApplicationSleep(FALSE);
  339.  
  340.     return 0;
  341. }
  342.  
  343. static ULONG __saveds mPrint( struct IClass *cl,
  344.                               Object *obj,
  345.                               Msg msg )
  346. {
  347.     PrintWindows(NULL);
  348.  
  349.     return 0;
  350. }
  351.  
  352. static ULONG __saveds mClose( struct IClass *cl,
  353.                               Object *obj,
  354.                               Msg msg )
  355. {
  356.     struct WindowsWinData *wwd = INST_DATA(cl, obj);
  357.     struct MUI_NListtree_TreeNode *tn;
  358.  
  359.     if (tn = GetActiveTreeNode(wwd->wwd_WindowTree)) {
  360.         struct WindowEntry *we = (struct WindowEntry *)tn->tn_User;
  361.  
  362.         if (stricmp(we->we_Type, "SCREEN") == 0) {
  363.             if (MyRequest(msgYesNo, msgWantToCloseScreen, we->we_Title)) {
  364.                 MyDoCommand("CloseScreen %s", we->we_Address);
  365.                 DoMethod(obj, MUIM_WindowsWin_Update);
  366.             }
  367.         } else if (stricmp(we->we_Type, "WINDOW") == 0) {
  368.             if (MyRequest(msgYesNo, msgWantToCloseWindow, we->we_Title)) {
  369.                 MyDoCommand("CloseWindow %s", we->we_Address);
  370.                 DoMethod(obj, MUIM_WindowsWin_Update);
  371.             }
  372.         }
  373.     }
  374.  
  375.     return 0;
  376. }
  377.  
  378. static ULONG __saveds mToFront( struct IClass *cl,
  379.                                 Object *obj,
  380.                                 Msg msg )
  381. {
  382.     struct WindowsWinData *wwd = INST_DATA(cl, obj);
  383.     struct MUI_NListtree_TreeNode *tn;
  384.  
  385.     if (tn = GetActiveTreeNode(wwd->wwd_WindowTree)) {
  386.         struct WindowEntry *we = (struct WindowEntry *)tn->tn_User;
  387.  
  388.         MyDoCommand("PopToFront %s", we->we_Address);
  389.     }
  390.  
  391.     return 0;
  392. }
  393.  
  394. static ULONG __saveds mMore( struct IClass *cl,
  395.                              Object *obj,
  396.                              Msg msg )
  397. {
  398.     struct WindowsWinData *wwd = INST_DATA(cl, obj);
  399.     struct MUI_NListtree_TreeNode *tn;
  400.  
  401.     if (tn = GetActiveTreeNode(wwd->wwd_WindowTree)) {
  402.         struct WindowEntry *we = (struct WindowEntry *)tn->tn_User;
  403.         APTR detailWin;
  404.  
  405.         if (detailWin = WindowsDetailWindowObject,
  406.                 MUIA_Window_Title, we->we_Title,
  407.                 MUIA_Window_ParentWindow, obj,
  408.                 MUIA_Window_MaxChildWindowCount, (opts.SingleWindows) ? 1 : 0,
  409.             End) {
  410.             set(detailWin, MUIA_WindowsDetailWin_Object, we);
  411.             set(detailWin, MUIA_Window_Open, TRUE);
  412.         }
  413.     }
  414.  
  415.     return 0;
  416. }
  417.  
  418. static ULONG __saveds mListChange( struct IClass *cl,
  419.                                    Object *obj,
  420.                                    Msg msg )
  421. {
  422.     struct WindowsWinData *wwd = INST_DATA(cl, obj);
  423.     struct MUI_NListtree_TreeNode *tn;
  424.  
  425.     if (tn = GetActiveTreeNode(wwd->wwd_WindowTree)) {
  426.         struct WindowEntry *we = (struct WindowEntry *)tn->tn_User;
  427.  
  428.         MySetContents(wwd->wwd_WindowText, "%s \"%s\"", we->we_Address, we->we_Title);
  429.         set(wwd->wwd_CloseButton, MUIA_Disabled, FALSE);
  430.         set(wwd->wwd_FrontButton, MUIA_Disabled, FALSE);
  431.         if (!clientstate) set(wwd->wwd_MoreButton, MUIA_Disabled, FALSE);
  432.     }
  433.  
  434.     return 0;
  435. }
  436.  
  437. ULONG __asm __saveds WindowsWinDispatcher( register __a0 struct IClass *cl,
  438.                                            register __a2 Object *obj,
  439.                                            register __a1 Msg msg )
  440. {
  441.     switch (msg->MethodID) {
  442.         case OM_NEW:                     return (mNew(cl, obj, (APTR)msg));
  443.         case OM_DISPOSE:                 return (mDispose(cl, obj, (APTR)msg));
  444.         case MUIM_WindowsWin_Update:     return (mUpdate(cl, obj, (APTR)msg));
  445.         case MUIM_WindowsWin_Print:      return (mPrint(cl, obj, (APTR)msg));
  446.         case MUIM_WindowsWin_Close:      return (mClose(cl, obj, (APTR)msg));
  447.         case MUIM_WindowsWin_ToFront:    return (mToFront(cl, obj, (APTR)msg));
  448.         case MUIM_WindowsWin_More:       return (mMore(cl, obj, (APTR)msg));
  449.         case MUIM_WindowsWin_ListChange: return (mListChange(cl, obj, (APTR)msg));
  450.     }
  451.  
  452.     return (DoSuperMethodA(cl, obj, msg));
  453. }
  454.  
  455. void PrintWindows( char *filename )
  456. {
  457.     BPTR handle;
  458.  
  459.     if (handle = HandlePrintStart(filename)) {
  460.         PrintFOneLine(handle, "\n  Address  Pos(x,y)  Size(x,y) Flags     IDCMP     Title\n\n");
  461.         IterateList(PrintCallback, (void *)handle);
  462.     }
  463.  
  464.     HandlePrintStop();
  465. }
  466.  
  467. void SendWinList( void )
  468. {
  469.     IterateList(SendCallback, NULL);
  470. }
  471.  
  472.